package com.jzy.bxb.model.util;

import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jzy.bxb.model.constant.AppConstant;

import push.AndroidNotification;
import push.PushClient;
import push.android.AndroidBroadcast;
import push.android.AndroidCustomizedcast;
import push.android.AndroidUnicast;
import push.ios.IOSBroadcast;
import push.ios.IOSCustomizedcast;

/**
 * UM推送工具
 *
 * @author JiangZhiYong
 * @date 2017-07-14 QQ:359135103
 */
public class UMPushUtil {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(UMPushUtil.class);
	private static PushClient client = new PushClient();
	private static final String APP_KEY = AppConstant.UM_APPKEY;
	private static final String APP_SECRET = AppConstant.UM_SECRET;

	/**
	 * 广播
	 * 
	 * @param ticker
	 *            提示
	 * @param title
	 *            标题
	 * @param text
	 *            文本
	 * @param consumer
	 *            额外参数 null--> 默认点击打开应用，显示类型为NOTIFICATION
	 * @author JiangZhiYong
	 * @date 2017-07-14 QQ:359135103
	 */
	public static void sendAndroidBroadcast(String ticker, String title,
			String text, Consumer<AndroidBroadcast> consumer) {
		try {
			AndroidBroadcast broadcast = new AndroidBroadcast(APP_KEY,
					APP_SECRET);
			broadcast.setTicker(ticker);
			broadcast.setTitle(title);
			broadcast.setText(text);
			broadcast.goAppAfterOpen();
			broadcast
					.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
			// TODO Set 'production_mode' to 'false' if it's a test device.
			// For how to register a test device, please see the developer doc.
			broadcast.setProductionMode();
			// broadcast.setTestMode();
			// Set customized fields
			// broadcast.setExtraField("test", "helloworld");
			if (consumer != null) {
				consumer.accept(broadcast);
			}
			client.send(broadcast);
		} catch (Exception e) {
			LOGGER.error("sendAndroidBroadcast", e);
		}
	}

	/**
	 * 唯一推送
	 * 
	 * @param ticker
	 *            提示
	 * @param title
	 *            标题
	 * @param text
	 *            文本
	 * @param consumer
	 *            额外参数 null--> 默认点击打开应用，显示类型为NOTIFICATION
	 * @throws Exception
	 * @author JiangZhiYong
	 * @date 2017-07-14 QQ:359135103
	 */
	public static void sendAndroidUnicast(String deviceToken, String ticker,
			String title, String text, Consumer<AndroidUnicast> consumer) {
		if (deviceToken == null) {
			return;
		}
		try {
			AndroidUnicast unicast = new AndroidUnicast(APP_KEY, APP_SECRET);
			unicast.setDeviceToken(deviceToken);
			unicast.setTicker(ticker);
			unicast.setTitle(title);
			unicast.setText(text);
			unicast.goAppAfterOpen();
			unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
			// TODO Set 'production_mode' to 'false' if it's a test device.
			// For how to register a test device, please see the developer doc.
			unicast.setProductionMode();
			// Set customized fields
			// unicast.setExtraField("test", "helloworld");
			if (consumer != null) {
				consumer.accept(unicast);
			}
			client.send(unicast);
		} catch (Exception e) {
			LOGGER.error("sendAndroidUnicast", e);
		}
	}

	/**
	 * 发送自定义广播
	 * 
	 * @throws Exception
	 */
	public static void sendAndroidCustomizedcast(String ticker, String title,
			String text, Consumer<AndroidCustomizedcast> consumer,
			String... alias) {
		try {
			AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(
					APP_KEY, APP_SECRET);
			if (alias == null || alias.length < 1) {
				return;
			}
			StringBuilder sb = new StringBuilder();
			for (String alia : alias) {
				sb.append(alia).append(",");
			}
			sb.substring(0, sb.length() - 1);

			// TODO Set your alias here, and use comma to split them if there
			// are multiple alias.
			// And if you have many alias, you can also upload a file containing
			// these alias, then
			// use file_id to send customized notification.
			customizedcast.setAlias(sb.toString(), "ID");
			customizedcast.setTicker(ticker);
			customizedcast.setTitle(title);
			customizedcast.setText(text);
			customizedcast.goAppAfterOpen();
			customizedcast
					.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
			// TODO Set 'production_mode' to 'false' if it's a test device.
			// For how to register a test device, please see the developer doc.
			customizedcast.setProductionMode();
			client.send(customizedcast);
		} catch (Exception e) {
			LOGGER.error("sendAndroidCustomizedcast", e);
		}
	}

	/**
	 * iOS推送
	 * 
	 * @param text
	 * @param consumer
	 * @param alias
	 */
	public static void sendIOSCustomizedcast(String text,
			Consumer<IOSCustomizedcast> consumer, String... alias) {
		try {
			IOSCustomizedcast customizedcast = new IOSCustomizedcast(
					AppConstant.UM_IOS_APPKEY, AppConstant.UM_IOS_SECRET);
			// TODO Set your alias and alias_type here, and use comma to split
			// them if there are multiple alias.
			// And if you have many alias, you can also upload a file containing
			// these alias, then
			// use file_id to send customized notification.
			if (alias == null || alias.length < 1) {
				return;
			}
			StringBuilder sb = new StringBuilder();
			for (String alia : alias) {
				sb.append(alia).append(",");
			}
			String string = sb.substring(0, sb.length() - 1);
			customizedcast.setAlias(string, "ID");
			customizedcast.setAlert(text);
			customizedcast.setBadge(0);
			customizedcast.setSound("default");
			// TODO set 'production_mode' to 'true' if your app is under
			// production mode
			customizedcast.setProductionMode();
			client.send(customizedcast);
		} catch (Exception e) {
			LOGGER.error("sendIOSCustomizedcast", e);
		}
	}

	/**
	 * ios 广播
	 */
	public static void sendIOSBroadcast(String text,
			Consumer<IOSBroadcast> consumer) {
		try {
			IOSBroadcast broadcast = new IOSBroadcast(
					AppConstant.UM_IOS_APPKEY, AppConstant.UM_IOS_SECRET);

			broadcast.setAlert(text);
			broadcast.setBadge(0);
			broadcast.setSound("default");
			// TODO set 'production_mode' to 'true' if your app is under
			// production mode
			broadcast.setProductionMode();
			// Set customized fields
			// broadcast.setCustomizedField("test", "helloworld");
			client.send(broadcast);
		} catch (Exception e) {
			LOGGER.error("sendIOSBroadcast", e);
		}

	}

}
